﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UCLibrary.UC
{
    [DefaultEvent("Click")]
    public partial class UCBtn : UserControl
    {
        public UCBtn()
        {
            InitializeComponent();
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);

        }
        private string _strON = "";
        [Category("自定/显示字符")]
        [Description("输入要显示的字符串")]
        public string str
        {
            get => _strON;
            set { _strON = value; Refresh(); }
        }
        private Font _font = new Font("微软雅黑", 15f, FontStyle.Bold);
        [Category("自定/显示字符")]
        [Description("显示字符的字体")]
        public Font font
        {
            get { return _font; }
            set { _font = value; Refresh(); }
        }
        private Color _strColor = Color.White;
        [Category("自定/显示字符")]
        [Description("显示字符的颜色")]
        public Color strColor
        {
            get => _strColor;
            set { _strColor = value; Refresh(); }
        }
        private Color _backgroundColor = Color.Green;
        [Category("自定/背景颜色")]

        [Description("按钮的背景颜色")]
        public Color backgroundColor
        {
            get { return _backgroundColor; }
            set { _backgroundColor = value; Refresh(); }
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            int f1, f2;
            f1 = Width;f2 = Height;
            OnPaintMain(g, f1, f2);
        }
        private void OnPaintMain(Graphics g,int width,int height)
        {
            int bigDiameter = height;
            GraphicsPath backgroundPath = new GraphicsPath();
            //从左开始画线，减1的目的是避免超出画板
            backgroundPath.AddArc(new Rectangle(0, 0, bigDiameter, bigDiameter - 1), 90, 180);//左边圆弧
            backgroundPath.AddLine(bigDiameter / 2, 0, width - bigDiameter / 2, 0);//中间上边直线
            backgroundPath.AddArc(new Rectangle(width - bigDiameter, 0, bigDiameter - 1, bigDiameter - 1), -90, 180);//右边圆弧
            backgroundPath.AddLine(width - bigDiameter / 2, height - 1, bigDiameter / 2, height - 1);//下边直线
            backgroundPath.CloseFigure();
            StringFormat format = new StringFormat();
            format.Alignment = StringAlignment.Center;
            format.LineAlignment = StringAlignment.Center;
            Function.setHighQuality(g);
            g.FillPath(new SolidBrush(backgroundColor), backgroundPath);
            if (a)
                g.DrawPath(Pens.Black, backgroundPath);
            g.DrawString(str, font, new SolidBrush(strColor), this.ClientRectangle, format);
        }
        private void UCBtn_MouseDown(object sender, MouseEventArgs e)
        {
            backgroundColor = Color.FromArgb(200,backgroundColor);
        }

        private void UCBtn_MouseCaptureChanged(object sender, EventArgs e)
        {
            backgroundColor = Color.FromArgb(255, backgroundColor);
        }
        bool a;
        private void UCBtn_MouseEnter(object sender, EventArgs e)
        {
            //BorderStyle = BorderStyle.FixedSingle;
            a = true;
            Refresh();
        }

        private void UCBtn_MouseLeave(object sender, EventArgs e)
        {
            a = false;
            Refresh();
            //BorderStyle = BorderStyle.None;
        }
    }
}
